package com.bk.cyxf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.object.IObject;
import jt.classic.system.object.ObjectLCManager;
import jt.classic.system.object.ObjectManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.Table;
import org.limp.basework.console.XSystemConfig;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DataTrimmerI;
import org.limp.mine.EncryptTool;
import org.limp.mine.StringTool;
import org.limp.mine.DataTrimmerI.DataItemList;



public class ViewjtxxAction extends DispatchAction {
    public ActionForward queryview(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        System.out.println( ISystem.getSessionUser(request));
        Connection conn = null;
        String id = StringTool.checkString(request.getParameter("id"));
        //IUser user = null;
        

        if (ISystem.invalidSesstion(request)) {
            IUser user = UserManager.createGuestUser();

            ISystem.createSessionUser(request, user);
        }

        try {
        	
        	SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);
            
            
            if (object.iobject() == null) {
                request.setAttribute("error", "来访登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lfid = StringTool.checkString(info.get("LFID"));

                String status = StringTool.checkString(info.get("LFSTATUS"));

                request.setAttribute("object", object);
                request.setAttribute("wobject", object.iobject());
                ISystem.createSessionUser(request, object.icreator());
                forward = mapping.findForward("queryview");

                
            }
        }
        
        catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }
    
    public ActionForward getExcelpwdtoJava(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        List<String> list_id = new ArrayList<String>(); 
        List<String> list_name = new ArrayList<String>();
        List<String> list_pwd = new ArrayList<String>();
        String REALPATH = (String) XSystemConfig.config().getProperty(XSystemConfig.XSYSTEM_REALPATH);
        File file = new File(REALPATH , "v_sys_user.xls");
        //String filePath = "D:/v_sys_user.xls";   
         
        Workbook workBook = null;

        try {
        	 //fs = new FileInputStream(filePath);   
        	 workBook = Workbook.getWorkbook(file);
        	 Sheet sheet = workBook.getSheet(0);//这里只取得第一个sheet的值，默认从0开始   
        	 /*System.out.println(sheet.getColumns());//查看sheet的列   
        	 System.out.println(sheet.getRows());//查看sheet的行*/        	 
        	 Cell cell = null;//就是单个单元格   
        	 
        	 for (int j = 0; j < sheet.getColumns(); j++) {   
                StringBuffer user_buffer_id =  new StringBuffer();
                StringBuffer user_buffer_name =  new StringBuffer();
                StringBuffer user_buffer_pwd =  new StringBuffer();
                HashMap my_user = new HashMap();
                for (int i = 0; i < sheet.getRows(); i++) {   
                     cell = sheet.getCell(j, i);
                     if(j==1){
                    	 user_buffer_id.append(cell.getContents());   
                    	 user_buffer_id.append(";");//将单元格的每行内容用逗号隔开
                    	 my_user.put("user_buffer_id", user_buffer_id);
                    	 
                     }
                     if(j==5){
                    	 user_buffer_name.append(cell.getContents());   
                    	 user_buffer_name.append(";");//将单元格的每行内容用逗号隔开
                    	 my_user.put("user_buffer_name", user_buffer_name);
                    	 
                     }
                     if(j==6){
                    	 user_buffer_pwd.append(cell.getContents());   
                    	 user_buffer_pwd.append(";");//将单元格的每行内容用逗号隔开
                    	 my_user.put("user_buffer_pwd", user_buffer_pwd);
                    	 
                     }
                     /*if(j==1 || j==5 || j==6){
                    	 user_buffer.append(cell.getContents());   
                    	 user_buffer.append(";");//将单元格的每行内容用逗号隔开
                     }*/
                        
                }   
                list_id.add(user_buffer_id.toString());//将每行的字符串用一个String类型的集合保存。
                list_name.add(user_buffer_name.toString());//将每行的字符串用一个String类型的集合保存。
                list_pwd.add(user_buffer_pwd.toString());//将每行的字符串用一个String类型的集合保存。
                
            }   
            workBook.close();//记得关闭 
            
            //迭代集合查看每行的数据
            String[] list_id_array = list_id.get(1).toString().split(";");
            String[] list_name_array = list_name.get(5).toString().split(";");
            String[] list_pwd_array = list_pwd.get(6).toString().split(";");
            
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();
            DataTrimmerI trimmer = new DataTrimmerI(conn);
            
            String sql = "UPDATE ISYS_USER SET PASSWORD1=? WHERE LOGID =?";
            for(int a=0;a<list_id_array.length;a++){
            	DataItemList dil = trimmer.createDIL();
            	dil.set(list_pwd_array[a]);
                dil.set(list_id_array[a]);
                trimmer.execute(sql, dil);
            }
            
            conn.commit();
            
            
        }
        catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } 
        catch (BiffException e) {   
               e.printStackTrace();   
        } 
        catch (IOException e) {   
               e.printStackTrace();   
        } 
        catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }
        System.out.println("更新密码明码成功");
        return forward;
    }
}
